<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/7
 * Time: 14:59
 */

namespace api\storage\service;


use api\storage\common\Constant;
use api\storage\model\StorageModel;
use think\Cache;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class StorageService
{
    /**
     * 更新仓库字段
     *
     * @param $bomUuid
     * @param $storageUuid
     * @return false|int
     */
    public static function setStorageWithBomUuid($bomUuid,$storageUuid){
        $storageModel = new StorageModel();
        return $storageModel->updateBomByUuid($bomUuid,$storageUuid);
    }

    /**
     * 获取仓库信息。注意只能获取那些不变的信息。比如说：仓库名。经常变的数据如：bomUuid不适合这种方式
     *
     * @param $storageUuid
     * @return array|mixed
     * @throws Exception
     */
    public static function getStorage($storageUuid){
        $storage = Cache::get(Constant::CACHE_PREFIX_STORAGE.$storageUuid);
        if(empty($storage)) {
            $storageModel = new StorageModel();
            try {
                $temp = $storageModel->findStorageByUuid($storageUuid);
            } catch (DataNotFoundException $e) {
                $temp = null;
            } catch (ModelNotFoundException $e) {
                $temp = null;
            } catch (DbException $e) {
                $temp = null;
            }
            if(empty($temp) || empty($temp->getData())){
                throw new Exception("根据仓库uuid:{$storageUuid},查找storage的数据为空");
            }
            $storage = $temp->toArray();
            //再缓存数据
            Cache::set(Constant::CACHE_PREFIX_STORAGE.$storageUuid,$storage);
        }
        return $storage;
    }
}